//
//  EditImageController.m
//  ImageFilter
//
//  Created by 冀秋羽 on 15/7/22.
//  Copyright (c) 2015年 冀秋羽. All rights reserved.
//

#import "EditImageController.h"
#import "ImageFilterItem.h"
#import "ImageUtil.h"
#import "ColorMatrix.h"
// 屏幕高度
#define HEIGHT [UIScreen mainScreen].bounds.size.height
// 屏幕宽度
#define WIDTH [UIScreen mainScreen].bounds.size.width
@interface EditImageController ()
@property(retain,nonatomic)ImageFilterItem *item;
@property(strong,nonatomic)UILabel *styleLable;//显示选中风格
@end

@implementation EditImageController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor darkGrayColor];
    [self loadImageView];
    [self initObject];

}
//隐藏电池栏
- (BOOL)prefersStatusBarHidden
{
    return YES;//隐藏为YES，显示为NO
}
//初始化UI
-(void)loadImageView{
//导航上方Button
    UIView *btnView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 40)];
    btnView.backgroundColor = [UIColor grayColor];
    
    UIButton *selectBtn = [[UIButton alloc]initWithFrame:CGRectMake(10, 0, 80, 40)];
    [selectBtn setTitle:@"重新选取" forState:UIControlStateNormal];
    if (self.iCare ==YES) {
        [selectBtn setTitle:@"取消" forState:UIControlStateNormal];
    }
     selectBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [selectBtn setTintColor:[UIColor whiteColor]];
    [selectBtn addTarget:self action:@selector(selectedBtn) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *useBtn = [[UIButton alloc]initWithFrame:CGRectMake(WIDTH-90, 0, 80, 40)];
    [useBtn setTitle:@"使用照片" forState:UIControlStateNormal];
    useBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [useBtn setTintColor:[UIColor whiteColor]];
    [useBtn addTarget:self action:@selector(useBtn) forControlEvents:UIControlEventTouchUpInside];
    
    [btnView addSubview:selectBtn];
    [btnView addSubview:useBtn];
//显示ImageView
    self.iconV = [[UIImageView alloc]init];
    self.iconV.frame =CGRectMake(30, 40, WIDTH-60, HEIGHT-40-120);
    self.iconV.image=self.image;
//选中样式名称
    self.styleLable = [[UILabel alloc]initWithFrame:CGRectMake(5, HEIGHT-145, 50, 20)];
    self.styleLable.textColor =[UIColor whiteColor];
    self.styleLable.text =@"原始";
    self.styleLable.font = [UIFont systemFontOfSize:12];
    self.styleLable.textAlignment =NSTextAlignmentCenter;
    self.styleLable.layer.borderColor =[[UIColor whiteColor]CGColor];
    self.styleLable.layer.borderWidth = 1;
    
    [self.view addSubview:btnView];
    [self.view addSubview:self.iconV];
    [self.view addSubview:self.styleLable];
}
//左上方Btn点击事件   重新选取or取消
-(void)selectedBtn{
    if (self.iCare ==YES) {
         [self dismissViewControllerAnimated:YES completion:nil];
    }else{
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentViewController:imagePicker animated:YES completion:nil];
    }

}
//右上方Btn点击事件  使用图片
-(void)useBtn{
     self.returnUseImageBlock(self.iconV.image,self.image);
    [self dismissViewControllerAnimated:YES completion:nil];
}
//相册代理方法
#pragma -mark ImagePickDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    [picker dismissViewControllerAnimated:NO completion:nil];
    self.iconV.image = image;
}
#define EFFECT_VIEW_SIZE 0
-(void)initObject{
    
    [self effectImages];
    NSArray *imageName = [NSArray arrayWithObjects:@"原始",@"lomo",@"黑白",@"怀旧",@"哥特",@"淡雅",@"酒红",@"清宁",@"浪漫",@"光晕",@"蓝调",@"梦幻",@"夜色", nil];
    self.scrollVIew = [[UIScrollView alloc]initWithFrame:CGRectMake(0, HEIGHT-120,WIDTH, 120)];
    _scrollVIew.contentSize=CGSizeMake(80*_effectImages.count,_scrollVIew.frame.size.height);
    [self.view addSubview:self.scrollVIew];
    for (int i =0; i<_effectImages.count; i++) {
        
        /**
         这里用if(!_item){},只会显示最后一个，item并不像一个cell会重用。
         */
        _item  = [[[NSBundle mainBundle] loadNibNamed:@"ImageFilterItem" owner:self options:nil] objectAtIndex:0];
        
        _item.frame=CGRectMake(i*80, 0,80, 120);
        _item.icon.image=_effectImages[i];
        _item.label_title.text=imageName[i];
        __weak typeof(self) weakSelf = self;
        _item.iconClick=^(){
            /**
             warn:
             Capturing 'self' strongly in this block is likely to lead to a retain cycle
             在代码块中使用对象的成员时(成员变量是属性strong，retain时):
             甲里引用乙，乙里引用甲，就会导致 “retain cycle” -- “形成怪圈”的错误
             解决的办法是 创建一个本地变量 指向 自己(self)
             */
            weakSelf.iconV.image=weakSelf.effectImages[i];
            weakSelf.styleLable.text =imageName[i];
        };
        [_scrollVIew addSubview:_item];
    }
}
//原始,lomo,黑白,怀旧,哥特,淡雅,酒红,清宁,浪漫,光晕,蓝调,梦幻,夜色
-(NSArray *)effectImages{
    if(!_effectImages)_effectImages = @[self.image,[ImageUtil imageWithImage:self.image withColorMatrix:colormatrix_lomo],[ImageUtil imageWithImage:self.image withColorMatrix:colormatrix_heibai],[ImageUtil imageWithImage:self.image withColorMatrix:colormatrix_huaijiu],[ImageUtil imageWithImage:self.image withColorMatrix:colormatrix_gete],[ImageUtil imageWithImage:self.image withColorMatrix:colormatrix_danya],[ImageUtil imageWithImage:self.image withColorMatrix:colormatrix_jiuhong],[ImageUtil imageWithImage:self.image withColorMatrix:colormatrix_qingning],[ImageUtil imageWithImage:self.image withColorMatrix:colormatrix_langman],[ImageUtil imageWithImage:self.image withColorMatrix:colormatrix_guangyun],[ImageUtil imageWithImage:self.image withColorMatrix:colormatrix_landiao],[ImageUtil imageWithImage:self.image withColorMatrix:colormatrix_menghuan],[ImageUtil imageWithImage:self.image withColorMatrix:colormatrix_yese]];
    return _effectImages;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}
@end
// 版权属于原作者
// http://code4app.com (cn) http://code4app.net (en)
// 发布代码于最专业的源码分享网站: Code4App.com
